export default {
  data () {
    return {
      rolesList: [],
      // 角色授权列表数据 ---------------
      // 是否显示角色授权列表
      isShowRolesRightTree: false,
      rolesRightsTree: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      // 角色id
      roleId: -1
    }
  },
  methods: {
  // 获取角色列表
    async getRolesList () {
      const res = await this.axios.get('roles')
      if (res.data.meta.status === 200) {
        this.rolesList = res.data.data
      }
    },
    indexMethod (i) {
      return i
    },
    // 删除角色权限
    async delRights (row, rightsId) {
      const res = await this.axios.delete(`roles/${row.id}/rights/${rightsId}`)
      console.log(res)
      const { data: { data, meta: { msg, status } } } = res
      if (status === 200) {
        this.$message({
          type: 'success',
          message: msg
        })
        row.children = data
      } else {
        this.$message({
          type: 'error',
          message: msg
        })
      }
    },
    // 展示角色授权列表树
    showRolesRightTree (row) {
      this.isShowRolesRightTree = true
      this.roleId = row.id
      const rightsArr = []
      row.children.forEach(item => {
        item.children.forEach(item => {
          item.children.forEach(item => {
            rightsArr.push(item.id)
          })
        })
      })
      console.log(rightsArr)
      this.$nextTick(function () {
        this.$refs.rightsTree.setCheckedKeys(rightsArr)
      })
    },
    async updateRolesRights () {
      const nodeArr = this.$refs.rightsTree.getCheckedKeys()
      const halfArr = this.$refs.rightsTree.getHalfCheckedKeys()

      const rids = [...nodeArr, ...halfArr].join(',')
      const res = await this.axios.post(`roles/${this.roleId}/rights`, {
        rids
      })
      const { data: { meta: { msg, status } } } = res
      if (status === 200) {
        this.$message({
          type: 'success',
          message: msg
        })
        this.isShowRolesRightTree = false
        this.getRolesList()
      } else {
        this.$message({
          type: 'error',
          message: msg
        })
      }
    }
    // 角色权限对话框关闭时触发
    // rolesRightsTreeClose () {
    //   this.hadRights = []
    // }
  },
  async created () {
    // 角色列表
    this.getRolesList()
    // 获取角色授权树数据
    const res = await this.axios.get('rights/tree')
    const { data: { data, meta: { status } } } = res
    if (status === 200) {
      this.rolesRightsTree = data
    }
  }
}
